
class_name UnitUI
extends CanvasLayer




## 结束行动信号
signal end_signal
## 攻击信号
signal attack_signal
## 移动信号
signal move_signal
## 技能信号
signal skill_signal




@onready var master: Unit = get_parent()
@onready var title = $Color/CR/Title
@onready var grid = $Color/CR/Scroll/Grid
@onready var items: Array = [
	$Color/CR/Scroll/Grid/Attack,
	$Color/CR/Scroll/Grid/Move,
	$Color/CR/Scroll/Grid/Hide,
	$Color/CR/Scroll/Grid/End
]




var pointer: int = 0
var current_item: TemplateSelectItem = null
var skill_item: Array = []




func start() -> void:
	
	title.text = "行动机会：" + str(master.property.action_chance) + \
		"\n攻击机会：" + str(master.property.attack_chance) + \
		"    移动机会：" + str(master.property.move_chance)
	
	pointer = 0
	current_item = items[ pointer ]
	current_item.set_select(true)
	set_process_input(true)
	read_skills()
	
	show()




func end() -> void:
	
	if current_item != null:
		
		current_item.set_select(false)
		current_item = null
	
	set_process_input(false)
	hide()




# 输入处理
func _input(_event: InputEvent) -> void:
	
	get_viewport().set_input_as_handled()
	
	#if _event.is_action_pressed("action_switch"):
	#	get_parent().hide_action_ui()
	#	pass
	
	if _event.is_action_pressed("pointer_up", true):
		
		if pointer - 2 >= 0:
			
			pointer -= 2
			current_item.set_select(false)
			current_item = items[ pointer ]
			current_item.set_select(true)
			set_process_input(true)
	
	elif _event.is_action_pressed("pointer_down", true):
		
		if pointer + 2 < items.size():
			
			pointer += 2
			current_item.set_select(false)
			current_item = items[ pointer ]
			current_item.set_select(true)
			set_process_input(true)
	
	elif _event.is_action_pressed("pointer_left", true):
		
		if pointer - 1 >= 0:
			
			pointer -= 1
			current_item.set_select(false)
			current_item = items[ pointer ]
			current_item.set_select(true)
			set_process_input(true)
	
	elif _event.is_action_pressed("pointer_right", true):
		
		if pointer + 1 < items.size():
			
			pointer += 1
			current_item.set_select(false)
			current_item = items[ pointer ]
			current_item.set_select(true)
			set_process_input(true)
	
	if _event.is_action_pressed("action_confirm"):
		
		action_confirm()




func action_confirm() -> void:
	
	var info = current_item.get_info_text()
	
	match info:
		
		"攻击":
			
			emit_signal("attack_signal")
		
		"移动":
			
			emit_signal("move_signal")
		
		"隐藏面板":
			
			end()
		
		"结束行动":
			
			emit_signal("end_signal")
		
		# 调用技能
		_:
			
			for sk in skill_item:
				
				if sk.save_object.skill_name == info:
					
					emit_signal("skill_signal", sk.save_object)
					return
	




## 读取技能列表
func read_skills() -> void:
	
	var sks = master.skill.skills
	
	for s in skill_item:
		
		items.erase(s)
		s.queue_free()
		
	skill_item.clear()
	
	# 加载技能
	for path in sks:
		
		var skl = master.skill.get_node(path)
		var temp = items[0].duplicate()
		$Color/CR/Scroll/Grid.add_child(temp)
		temp.set_info_text(skl.skill_name)
		temp.set_select(false)
		temp.save_object = skl
		
		items.append(temp)
		skill_item.append(temp)
